﻿$(document).ready(function () {

    var oTableFinding = $('#finding').dataTable({
        "iDisplayLength": 15,
        "bProcessing": true,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bRetrieve": true,
        "sAjaxSource": "/_vti_bin/WCFService.svc/GetFinding",
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $.each(aData, function (index, value) {
                $('td:eq(' + index + ' )', nRow).html("<div class='wrap'>" + aData[index] + "</div>");
            });

            return nRow;
        },
        "fnServerParams": function (aoData) {
            fnServerParams(aoData);
        }
    });

    $("#addFinding").click(function () {
        var url = "/_layouts/HolcimProject/FindingFlowPage.aspx";
        OpenDialogSharePoint(url, "Finding(s)", 600);
    });


});

function fnServerParams(aoData) {

}

function OpenDialogEditTask(findingid, findingtaskid, state) {
    var url = "/_layouts/HolcimProject/FindingFlowPage.aspx?findingtaskid=" + findingtaskid + "&flat=true";

    OpenDialogSharePoint(url, "To do", 600);
}

function OpenDialogSharePoint(url, title, width) {
    var options = {
        url: url,
        title: title,
        width: width,
        allowMaximize: false,
        showClose: true,
        dialogReturnValueCallback: DialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function DialogCallback(dialogResult, returnValue) {
    RefreshTable('#finding', '/_vti_bin/WCFService.svc/GetFinding');
}

function RefreshTable(tableId, urlData) {
    $.getJSON(urlData, null, function (json) {
        table = $(tableId).dataTable();
        oSettings = table.fnSettings();
        table.fnClearTable(this);

        for (var i = 0; i < json.aaData.length; i++) {
            table.oApi._fnAddData(oSettings, json.aaData[i]);
        }
        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        table.fnDraw();
    });
}